﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using HandyControl.Controls;

namespace YJTMedicalAideManage.Helper
{
    public static class PasswordHelper
    {
        // 创建附加属性 Password
        public static readonly DependencyProperty PasswordProperty =
            DependencyProperty.RegisterAttached(
                "Password",
                typeof(string),
                typeof(PasswordHelper),
                new PropertyMetadata(string.Empty, OnPasswordChanged));

        // 设置附加属性的值
        public static void SetPassword(UIElement element, string value)
        {
            element.SetValue(PasswordProperty, value);
        }

        // 获取附加属性的值
        public static string GetPassword(UIElement element)
        {
            return (string)element.GetValue(PasswordProperty);
        }

        // 当密码发生变化时执行的回调
        private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is PasswordBox passwordBox)
            {
                // 解除事件订阅，避免重复订阅
                passwordBox.PreviewTextInput -= PasswordBox_PasswordChanged;

                // 设置密码
                passwordBox.Password = (string)e.NewValue;

                // 重新订阅事件
                passwordBox.PreviewTextInput += PasswordBox_PasswordChanged;
            }
        }

        // 处理密码改变事件
        private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (sender is PasswordBox passwordBox)
            {
                SetPassword(passwordBox, passwordBox.Password); // 将密码设置到附加属性
            }
        }
    }


}
